package SQL.scan;

import heap.ResultRow;
import heap.ResultSchema;
import SQL.abysnSQL.BoolCondition;
import SQL.valueUtil.BoolExprVerify;
import Transaction.Transaction;

/**
 * 
 * @author Azure
 *
 */

public class SelectScan implements Scan {

	public SelectScan(Scan s, String db, BoolCondition where,Transaction tx)throws Exception{
		this.s = s;
		this.db = db;
		this.where = where;
		this.tx = tx;
		this.schema = s.getSchema();
	}
	@Override
	public void beforeFirst() throws Exception {
		s.beforeFirst();
	}

	@Override
	public void close() throws Exception {
		s.close();
	}

	@Override
	public ResultRow getCurrent() throws Exception {
		return result;
	}

	@Override
	public ResultSchema getSchema() throws Exception {
		return schema;
	}

	@Override
	public boolean hasField(String fldname) throws Exception {
		return s.hasField(fldname);
	}

	@Override
	public ResultRow next() throws Exception {
		ResultRow next = s.next();
		while(next != null){
			BoolExprVerify bVerify = new BoolExprVerify(where, db, schema, next, tx);
			if(bVerify.verify()){
				result = next;
				return result;
			}
			next = s.next();
		}
		return null;
	}

	Scan s;
	String db;
	BoolCondition where;
	Transaction tx;

	ResultRow result;
	ResultSchema schema;
}
